package com.jnu.myaccount_drawer.ui.manage;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jnu.myaccount_drawer.R;
import com.jnu.myaccount_drawer.data.AccountType;
import com.jnu.myaccount_drawer.data.DataBank;

import java.util.List;

public class ManageFragment extends Fragment{

    ActivityResultLauncher<Intent> launcherPayType = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {

    });
    private DataBank dataBank;
    private List<AccountType> accountType;
    private double grossAccount;
    private double grossCash;
    private double grossWechat;
    private double grossAlipay;

    public ManageFragment() {
        // Required empty public constructor
    }

    public static ManageFragment newInstance() {
        ManageFragment fragment = new ManageFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_manage, container, false);

        initData();

        Button buttonCash=rootView.findViewById(R.id.button_cash);
        Button buttonWechat=rootView.findViewById(R.id.button_wechat);
        Button buttonAlipay=rootView.findViewById(R.id.button_alipay);

        buttonCash.setOnClickListener(view -> {
            Intent intent=new Intent(ManageFragment.this.getContext(), CashActivity.class);
            launcherPayType.launch(intent);
        });
        buttonWechat.setOnClickListener(view -> {
            Intent intent=new Intent(ManageFragment.this.getContext(), WechatActivity.class);
            launcherPayType.launch(intent);
        });
        buttonAlipay.setOnClickListener(view -> {
            Intent intent=new Intent(ManageFragment.this.getContext(), AlipayActivity.class);
            launcherPayType.launch(intent);
        });

        //净资产
        TextView textViewNetAssetsPrice=rootView.findViewById(R.id.text_view_net_assets_price);
        for(int i=0; i <accountType.size(); i++)
        {
            grossAccount+=accountType.get(i).getPrice();//净资产
        }
        textViewNetAssetsPrice.setText(grossAccount+"");

        TextView textViewCash=rootView.findViewById(R.id.text_view_cash);
        TextView textViewWechat=rootView.findViewById(R.id.text_view_wechat);
        TextView textViewAlipay=rootView.findViewById(R.id.text_view_alipay);
        for(int i=0; i <accountType.size(); i++)
        {
            if(accountType.get(i).getPayType().equals("现金"))
            {
                grossCash+=accountType.get(i).getPrice();
            }
            if(accountType.get(i).getPayType().equals("微信"))
            {
                grossWechat+=accountType.get(i).getPrice();
            }
            if(accountType.get(i).getPayType().equals("支付宝"))
            {
                grossAlipay+=accountType.get(i).getPrice();
            }
        }
        textViewCash.setText(grossCash+"");
        textViewWechat.setText(grossWechat+"");
        textViewAlipay.setText(grossAlipay+"");

        return rootView;
    }

    private void initData() {
        dataBank = new DataBank(this.getContext());
        accountType= dataBank.loadData();
    }
}